package com.test.sdk.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 徒有琴
 */
public class TicketUtil {
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");

    //^ a^b=c  c^b=a
    public static String encode(String ticket) {
        if (ticket == null) {
            return null;
        }
        StringBuilder builder = new StringBuilder();
        //yyyyMMddHHmmss
        //abcdefghijklmnopqrstuvwxyz
        Date now = new Date();
        String time = format.format(now);
        for (int i = 0; i < 14; i++) {
            builder.append(ticket.charAt(i));
            builder.append(time.charAt(i));
        }
        builder.append(ticket.substring(14));
        return builder.toString();
    }

    public static void main(String[] args) {
        String url="/common/hear.html";
        System.out.println(url.matches("/user/.*"));
        System.out.println(2 ^ 3);
        System.out.println(Integer.toHexString(6 ^ 9));
        System.out.println(Integer.valueOf("f", 16));
        System.out.println(15 ^ 9);
        System.out.println(encode("abcdefghijklmnopqrstuvwxyz"));
        System.out.println(decode("a2b0c1d9e0f9g0h4i1j1k2l5m4n6opqrstuvwxyz"));
        System.out.println(getTime("a2b0c1d9e0f9g0h4i1j1k2l8m5n7opqrstuvwxyz"));
    }

    public static String decode(String ticket) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 14; i++) {
            builder.append(ticket.charAt(2 * i));
        }
        builder.append(ticket.substring(28));
        return builder.toString();
    }

    public static Date getTime(String ticket) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 14; i++) {
            builder.append(ticket.charAt(2 * i + 1));
        }
        String time = builder.toString();
        try {
            return format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
